package com.yc.forum.servlet;

import com.yc.forum.dao.UserDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.*;
import java.io.IOException;

//登录
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        UserDao userDao=new UserDao();
        HttpSession session=request.getSession();

        String name=request.getParameter("login_phone");
        String pwd=request.getParameter("login_pwd");
        System.out.println("登录人:"+session.getAttribute("loginedUser"));
        try {
            session.getAttribute("loginedUser").equals("");
            response.getWriter().write("已登录");
            return;
        }catch(NullPointerException e){
            System.out.println("未登录");
        }
        if(userDao.querylogin(name,pwd)){
            session.setAttribute("loginedUser",name);
            System.out.println("登录成功！！");
            response.sendRedirect("index.html");
        }else{
            System.out.println("用户名或密码错误！！");
            response.getWriter().write("用户名或密码错误！！");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
